using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DogController : MonoBehaviour
{
    private Animator animator;

    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    private void Awake()
    {
        animator = GetComponent<Animator>();
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.name == Defines.MainCharacterName)
        {
            var currentQuest = QuestSystem.Instance.CurrentQuest;
            if (currentQuest is SootheDog && currentQuest.Status == QuestStatus.InProgress)
            {
                LunaStatusManager.Instance.LunaState = LunaState.Dialog;
                StartCoroutine(SootheDogCoroutine(collision));
            }
        }
    }

    IEnumerator SootheDogCoroutine(Collider2D collision)
    {
        collision.transform.position = new Vector2(-0.8f, -7.9f);
        var animatorLuna = collision.GetComponent<Animator>();
        animatorLuna.SetBool(Defines.Soothe, true);
        this.animator.SetBool(Defines.Happy, true);
        yield return new WaitForSeconds(1.5f);
        animatorLuna.SetBool(Defines.Soothe, false);
        collision.transform.position = new Vector2(-0.9f, -7.9f);
        QuestSystem.Instance.CurrentQuest.Status = QuestStatus.Completed;
        GameManager.Instance.PlaySoundCompleteQuest();
        LunaStatusManager.Instance.LunaState = LunaState.Normal;
        /* 停止并禁用音效 */
        gameObject.GetComponentInChildren<AudioSource>().Stop();
        Transform[] transforms = gameObject.GetComponentsInChildren<Transform>();
        foreach (Transform temp in transforms)
        {
            if (temp == transform)
                continue;
            Destroy(temp.gameObject);
        }
    }
}
